import isEmpty from 'loadsh/isEmpty'
import { GetInfoPicture, GetMeetingRoomInfo, GetPreviewImage, PostDeleteData, PostInfoPicture, PostMeetingRoomInfo } from '/constData/requestFun/_httpRequest';

Page({
  data: {
    item: 1,
    min: null,
    max: new Date('2033/12/02'),
    defaultDate: new Date(),
    meetroominfo: {
      CorpId: '',//申请人
      AffiliatedCompany: '',//所属公司
      ApplicationTime: '', //申请时间
      UsageTime: '',//使用时间
      ReasonBorrow: '',//借用事由
      Annex: [],//附件
    },
    Name: '',
    ReceiveData: [],
  },
  Delete(e) {
    const ID = e.target.dataset.item.id
    PostDeleteData(ID, 'Meeting', (res) => {
      my.alert({
        title: '撤销成功',
      });
      //刷新数据
      this.setData({
        ReceiveData: this.data.ReceiveData.filter((it) => {
          return it.id !== ID
        })
      })
    }, (err) => {
      my.alert({
        title: '撤销失败',
      });
    })

  },
  handleOk2(date, format, e) {
    this.setData({
      meetroominfo: {
        ...this.data.meetroominfo,
        ApplicationTime: date
      },
    })
  },
  handleOk1(date, format, e) {
    this.setData({
      meetroominfo: {
        ...this.data.meetroominfo,
        UsageTime: date
      },
    })
  },
  onReasonBorrowChange(e) {
    this.setData({
      meetroominfo: {
        ...this.data.meetroominfo,
        ReasonBorrow: e
      }
    })
  },
  onAffiliatedCompanyChange(e) {
    this.setData({
      meetroominfo: {
        ...this.data.meetroominfo,
        AffiliatedCompany: e
      }
    })
  },
  // 上传数据
  onSubmit(e) {
    PostMeetingRoomInfo(
      JSON.stringify(this.data.meetroominfo),
      (res) => {
        my.alert({
          title: '上传成功',
        });
        GetMeetingRoomInfo((res) => {
          console.log('res', res);
          this.setData({
            ReceiveData: res.data.data
          })
        }, this.data.meetroominfo.CorpId)

        //清除填写数据
        this.setData({
          meetroominfo: {
            ...this.data.meetroominfo,
            AffiliatedCompany: '',//所属公司
            ApplicationTime: '', //申请时间
            UsageTime: '',//使用时间
            ReasonBorrow: '',//借用事由
            Annex: [],//附件
          },
        })
      },
      (err) => {
        my.alert({
          title: '提交成功1',
        });
      }
    )

  },
  //上传图片
  onUpload(file) {
    const filePath = file.path
    return new Promise((resolve, reject) => {
      PostInfoPicture(filePath, (res) => {
        this.setData({
          meetroominfo: {
            ...this.data.meetroominfo,
            Annex: [...this.data.meetroominfo.Annex, ...JSON.parse(res.data).data]
          }
        })
      })
    })
  },
  onRemove(file) {
    return new Promise((resolve) => {
      console.log('即将移除的图片为：', file);
      my.confirm({
        title: '移除图片',
        content: '是否确认移除图片',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (e) => {
          resolve(e.confirm);
        }
      });
    })
  },
  onBeforeUpload(localFileList) {
    console.log('即将上传的图片列表为：', localFileList);
    localFileList = localFileList.filter(item => item.size < 10000);
    console.log('修改上传的图片列表为：', localFileList);
    return localFileList;
  },
  onChooseImageError(err) {
    console.log(err);
  },
  //获取数据
  onLoad() {
    my.getStorage({
      key: 'userInfo',
      success: (res) => {
        GetMeetingRoomInfo((res) => {
          this.setData({
            ReceiveData: res.data.data
          })
        }, res.data.result.userid)
        this.setData({
          meetroominfo: {
            ...this.data.meetroominfo,
            CorpId: res.data.result.userid
          },
          Name: res.data.result.name
        })
      }
    })
  },
  //图片下载函数
  DownImg(e) {
    const imgNames = e.currentTarget.dataset.annex.split(',');
    if (isEmpty(e.currentTarget.dataset.annex)) {
      my.alert({ content: '无附件图片！' })
    } else {
      Promise.all(imgNames.map(imgName =>
        new Promise((resolve) => {
          GetInfoPicture(imgName, (res) => {
            resolve(res.filePath)
          })
        })
      )).then(res => {
        GetPreviewImage(res)
      })
    }
  },

  bindButtonTap() {
    this.onFocus();
  },
  bindTextAreaBlur(e) {
    console.log(e.detail.value);
  },
});